package test;

import java.io.StringWriter;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
 
 
public class CriaXML {
	public static void main(String[] args) {
		
		int numeroCampos = Integer.parseInt(args[0]);
		int tamanhoHierarquia = Integer.parseInt(args[1]);
		int tamanhoCampos = Integer.parseInt(args[2]);
		
		/*int numeroCampos = 5;
		int tamanhoHierarquia = 1000;
		int tamanhoCampos = 5;*/
		
		String valorCampo = "";
		
		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			
			Document doc = docBuilder.newDocument();
			
			long start = new Date( ).getTime( );
			Element rootElement = doc.createElement("obj" + tamanhoHierarquia);
			Element tempElement;
			

			for(int i = 1; i <= numeroCampos; i++){
				for(int j = 1; j<= tamanhoCampos; j++){
					valorCampo += "a";
				}
				tempElement = doc.createElement("campo" + i);
				tempElement.setTextContent(valorCampo);
				
				rootElement.appendChild(tempElement);
				valorCampo = "";
			}
			for(int i = tamanhoHierarquia-1; i >= 1; i--){
				docBuilder.newDocument().createElement("obj" + i);//simula criacao de nova estrutura
				tempElement = doc.createElement("obj" + i);
				tempElement.appendChild(rootElement);
				rootElement = tempElement;
			}
			
			doc.appendChild(rootElement);			
			
			TransformerFactory tf = TransformerFactory.newInstance();
			try {
				Transformer t = tf.newTransformer();
				DOMSource source = new DOMSource(doc);
				StringWriter sw = new StringWriter();
				StreamResult result = new StreamResult(sw);
				t.transform(source, result);
				sw.toString();//extrai msg em modo texto
			} catch (TransformerConfigurationException e) {
				e.printStackTrace();
			} catch (TransformerException e) {
				e.printStackTrace();
			}
			
			long end = new Date().getTime();
			System.out.println(end - start);
			
		} catch (ParserConfigurationException e) {
			System.out.println("error on document builder");
		}
 
	}
}
